package com.example.inventory.controller;

import com.example.inventory.api.InventoryApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 库存微服务
 *
 * @author james
 */
@RestController
public class InventoryController implements InventoryApi {

    @Value("${test.sleep}")
    private Long sleepSecond;

    @Override
    @RequestMapping(value = "/deduct/{productId}/{stock}", method = RequestMethod.PUT)
    public String deductStock(@PathVariable("productId") Long productId, @PathVariable("stock") Long stock) {
        System.out.println("库存微服务已经对商品id为" + productId + "的商品减少了" + stock + "个数量");
        String result = String.format("库存微服务已经对商品id为%d的商品减少了%d个数量", productId, stock);
        System.out.println(result);
        // 这样就会厨房降级了
        try {
            Thread.sleep(sleepSecond);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }
}
